package idiet.wizards.page;

import idiet.Activator;
import idiet.bean.Nation;
import idiet.service.NationService;
import idiet.util.Validate;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import swing2swt.layout.BoxLayout;

public class WizardPageCustomerBasicInfo extends WizardPage {
	private Logger logger=Logger.getLogger(WizardPageCustomerBasicInfo.class);
	private String name;
	private Text textName;
	private Text textPhone;
	private Text textCellphone;
	private Text textIm;
	private Text textEmail;
	private Text textPostalAddress;
	private Text textComment;
	private Button buttonMale;
	private Button buttonFemale;
	private Combo comboNation;
	private String tips="\u6DFB\u52A0\u5BA2\u6237\u7684\u59D3\u540D\u3001\u6027\u522B\u3001\u6C11\u65CF\u3001\u7535\u8BDD\u3001QQ\u3001\u7535\u5B50\u90AE\u4EF6\u3001\u901A\u8BAF\u5730\u5740\u7B49\u4FE1\u606F";
	private NationService nationService=(NationService)Activator.getApplicationContext().getBean("nationService");
	//private DialogSettings settings=new DialogSettings("settings");
	//private CustomerService customerService=Activator.getApplicationContext().getBean(CustomerService.class);
	/**
	 * Create the wizard.
	 */
	public WizardPageCustomerBasicInfo() {
		super("wizardPage");
		setTitle("\u7B2C\u4E00\u6B65\uFF1A\u6DFB\u52A0\u5BA2\u6237\u57FA\u672C\u4FE1\u606F");
		setDescription(tips);
		//this.setErrorMessage(null);
		this.setPageComplete(false);
	}
	
	@Override
	public IWizardPage getNextPage() {
		// TODO Auto-generated method stub
		if(textName.getText().trim().equals("")){
			setErrorMessage("customer's name must not be empty !");
			setPageComplete(false);
			return this;
		}
		if(textPhone.getText().trim().equals("")&&textCellphone.getText().trim().equals("")){
			setErrorMessage("At least one phone number !");
			setPageComplete(false);
			return this;
		}
		
		WizardPageCustomerPhysiologicInfo next=(WizardPageCustomerPhysiologicInfo)super.getNextPage();
		next.setName(this.textName.getText());
		return super.getNextPage();
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		GridLayout gl_container = new GridLayout(2, false);
		gl_container.marginRight = 50;
		container.setLayout(gl_container);
		
		Label label = new Label(container, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setAlignment(SWT.RIGHT);
		label.setText("\u59D3\u540D\uFF1A");
		
		textName = new Text(container, SWT.BORDER);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textName.getText().trim().equals("")){
					setErrorMessage("customer's name must not be empty !");
					setPageComplete(false);
				}else{
					setPageComplete(true);
					setErrorMessage(null);
					setMessage(tips);
				}
			}
		});
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setAlignment(SWT.RIGHT);
		label_1.setText("\u6027\u522B\uFF1A");
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new BoxLayout(BoxLayout.X_AXIS));
		
		buttonMale = new Button(composite, SWT.RADIO);
		buttonMale.setSelection(true);
		buttonMale.setText("\u7537");
		
		buttonFemale = new Button(composite, SWT.RADIO);
		buttonFemale.setText("\u5973");
		
		Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setAlignment(SWT.RIGHT);
		label_2.setText("\u6C11\u65CF\uFF1A");
		
		comboNation = new Combo(container, SWT.READ_ONLY);
		GridData gd_comboNation = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_comboNation.widthHint = 109;
		comboNation.setLayoutData(gd_comboNation);
		List<Nation> nations=nationService.getNationsByNationName(null);
		for(Nation nation:nations){
			comboNation.add(nation.getName());
			comboNation.setData(nation.getName(),nation.getId());
		}
		
		Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setAlignment(SWT.RIGHT);
		label_3.setText("\u56FA\u5B9A\u7535\u8BDD\uFF1A");
		
		textPhone = new Text(container, SWT.BORDER);
		textPhone.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				setPageComplete(true);
				setErrorMessage(null);
				setDescription(tips);
			}
		});
		textPhone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setAlignment(SWT.RIGHT);
		label_4.setText("\u79FB\u52A8\u7535\u8BDD\uFF1A");
		
		textCellphone = new Text(container, SWT.BORDER);
		textCellphone.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				setPageComplete(true);
				setErrorMessage(null);
				setDescription(tips);
			}
		});
		textCellphone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblqqmsn = new Label(container, SWT.NONE);
		lblqqmsn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblqqmsn.setAlignment(SWT.RIGHT);
		lblqqmsn.setText("\u5373\u65F6\u901A\u8BAF\uFF08QQ\uFF0CMSN\uFF09\u7B49\uFF1A");
		
		textIm = new Text(container, SWT.BORDER);
		textIm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setAlignment(SWT.RIGHT);
		label_5.setText("\u7535\u5B50\u90AE\u4EF6\uFF1A");
		
		textEmail = new Text(container, SWT.BORDER);
		textEmail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				if(Validate.emailValidate(textEmail.getText())){
					setErrorMessage("customer's name must not be empty !");
					setPageComplete(false);
				}else{
					setPageComplete(true);
					setErrorMessage(null);
					setMessage(tips);
				}
			}
		});
		textEmail.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setAlignment(SWT.RIGHT);
		label_6.setText("\u901A\u8BAF\u5730\u5740\uFF1A");
		
		textPostalAddress = new Text(container, SWT.BORDER);
		textPostalAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(container, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_7.setAlignment(SWT.RIGHT);
		label_7.setText("\u5907\u6CE8\uFF1A");
		
		textComment = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_textComment.heightHint = 45;
		textComment.setLayoutData(gd_textComment);
	}

	@Override
	public IDialogSettings getDialogSettings() {
		// TODO Auto-generated method stub
		DialogSettings settings=new DialogSettings("settings");
		settings.put("name", textName.getText());
		if(buttonFemale.getSelection()){
			settings.put("gender", "女");
		}else{
			settings.put("gender", "男");
		}
		if(!comboNation.getText().trim().equals("")){
			settings.put("nation", comboNation.getData(comboNation.getText()).toString());
		}else{
			settings.put("nation", comboNation.getData("汉族").toString());
		}
		settings.put("phone", textPhone.getText());
		settings.put("cellphone", textCellphone.getText());
		settings.put("im", textIm.getText());
		settings.put("email", textEmail.getText());
		settings.put("address", textPostalAddress.getText());
		settings.put("comment", textComment.getText());
		return settings;
	}

	public void setName(String name) {
		this.name = name;
		this.textName.setText(name);
		this.textName.setEnabled(false);
	}
	
}
